package QianFeng04;

/**
 * 适配器模式:
 * 适配器模式( Adapter) :将一个类的接口转换成客户希望的另外一个接口。
 * 适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。(转接口)
 **/
public class Test12 {
    public static void main(String[] args) {
        PowerA powerA = new PowerAImpl();
        work(powerA);
        System.out.println("--------------------");
        //利用适配器转换输出powerB
        PowerB powerB = new PowerBImpl();
        Adapter adapter = new Adapter(powerB);
        work(adapter);
    }

    public static void work(PowerA a) {
        System.out.println("正在连接...");
        a.insert();
        System.out.println("工作结束.");
    }
}

interface Animal1{
    void sing();
    void cry();
    void run();
    void swim();
}
interface PowerA{
    void insert();
}
interface PowerB{
    void connect();
}

class PowerAImpl implements PowerA{

    @Override
    public void insert() {
        System.out.println("电源A开始工作...");
    }
}

class PowerBImpl implements PowerB {

    @Override
    public void connect() {
        System.out.println("电源B开始工作...");
    }
}
//适配器，转换为PowerA类型
class Adapter implements PowerA{
    private PowerB powerB;

    public Adapter(PowerB powerB) {
        this.powerB = powerB;
    }
    @Override
    public void insert() {
        powerB.connect();
    }
}

//适配器类，使Dog1类不用全部写出接口方法
abstract class AnimalFunction implements Animal1{
    public void sing(){};
    public void cry(){};
    public void run(){};
    public void swim(){};
}
class Dog1 extends AnimalFunction{
    public void run() {
        System.out.println("我是疯狗，疯狂的跑");
    }

}